$(function () {
    $("#jqGrid").jqGrid({
        url:  baseURL + 'sys/customer/list', //默认先查点位模式和进行中的订单
        datatype: "json",
         colModel: [			
			{ label: 'id', name: 'id',index: "id", /*width:20,*/ key: true ,hidden:true},
			{ label: '用户', name: '', width: 250, 
				  formatter: function (cellvalue, options, rowObject) { 
					  var a = rowObject.username+"【"+rowObject.cusname+"】";
					  return "<a href=\"#\" onclick=\"upcust(" + options.rowId + ")\">"+a+"</a>" ;
			     }  
			},
			{ label: '上级',sortable:false, name: 'sjname', width: 200, formatter: function(value, options, row){
				var sjusername = row.sjusername;
				var sjname = row.sjname;
				if(isBlank(sjusername)){
					sjusername = " ";
				}
				if(isBlank(sjname)){
					sjname = " ";
				}
				
				return sjusername+"【"+sjname+"】"
			}},
			{ label: '账户余额',sortable:false, name: 'candycount', width: 200},
			{ label: '充值总数',sortable:false, name: 'czcount', width: 120},
			{ label: '交易总数',sortable:false, name: 'ordercount', width: 150},
			{ label: '手续费总数',sortable:false, name: 'sumcharge', width: 150},
			{ label: '注册日期',sortable:false, name: 'creattime', width: 200 },
			{ label: '最近登入',sortable:false, name: 'logintime', width: 200},
			{ label: '状态', name: 'type', width: 100, formatter: function(value, options, row){
				return value === 2 ? 
					"<a class='label label-danger' href=\"#\" onclick=\"upuserstatus(" + options.rowId +","+row.type+ ")\">禁用</a>" : 
					"<a class='label label-success' href=\"#\" onclick=\"upuserstatus(" + options.rowId +","+row.type+ ")\">正常</a>";
			}},
			{  label: '操作',sortable:false, name: 'cz', index: 'cz', width: 100,align: 'center',
			     formatter: function (cellvalue, options, rowObject) { 
			     	return "<a  class='fa fa-pencil-square-o' href=\"#\" style=\"color:#f60\" onclick=\"Modify(" + options.rowId + ")\">查看团队</a>" 
			     }  
			  },
			  
        ],
		viewrecords: true,
        height: 500,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        multiselect: false,
        autoScroll: true,
        shrinkToFit: false,//此属性用来说明当初始化列宽度时候的计算类型，如果为ture，则按比例初始化列宽度。如果为false，则列宽度使用colModel指定的宽度
        autowidth: true, //如果为ture时，则当表格在首次被创建时会根据父元素比例重新调整表格宽度。如果父元素宽度改变，为了使表格宽度能够自动调整则需要实现函数：setGridWidth
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
       	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" }); 
        }
    });
	
});
function upcust(id) {   
	$.ajax({
			type: "POST",
		    url: baseURL + "sys/customer/queryById",
		    data: {"id":id},
		    success: function(r){
				if(r.code == 0){
					$("#userid").val(r.customerEntity.id);
					$("#username").val(r.customerEntity.username);
					$("#cusname").val(r.customerEntity.cusname);
					$("#invitecode").val(r.customerEntity.invitecode);
					$("#candycount").val(r.customerEntity.candycount);
				}else{
					alert(r.msg);
				}
			}
		});
	$.ajax({
		type: "POST",
		url: baseURL + "sys/customer/queryBankCard",
		data: {"id":id},
		success: function(r){
			if(r.code == 0){
				var html = "";
				for (var int = 0; int < r.list.length; int++) {
					if(int == 0){
						$("#bankid").val(r.list[int].id);
						$("#bankcardname").val(r.list[int].bankCardName);
						$("#openbranch").val(r.list[int].openBranch);
						$("#peraccount").val(r.list[int].perAccount);
					}
					html += "<option value="+r.list[int].id+">"+r.list[int].bankAccount+"</option>";
				}
				document.getElementById("bankaccount").innerHTML = html;
			}else{
				alert(r.msg);
			}
		}
	});
	$("#upcus").modal("show");
} 
function queryBank(){
	var carid = $("#bankaccount").val();
	$.ajax({
		type: "POST",
		url: baseURL + "sys/customer/queryBankCardById",
		data: {"carid":carid},
		success: function(r){
			if(r.code == 0){
				$("#bankid").val(r.bankCardEntity.id);
				$("#bankcardname").val(r.bankCardEntity.bankCardName);
				$("#openbranch").val(r.bankCardEntity.openBranch);
				$("#peraccount").val(r.bankCardEntity.perAccount);
			}else{
				alert(r.msg);
			}
		}
	});
}
function upuserstatus(id,value) {  
	var userStatus;
	if(value == 1){
		userStatus = 2;
	}else{
		userStatus = 1;
	}
	confirm('确定要更改当前状态吗？', function(){
		$.ajax({
			type: "POST",
		    url: baseURL + "sys/customer/upuserstatus",
		    data: {"id":id,"type":userStatus},
		    success: function(r){
				if(r.code == 0){
					alert('操作成功', function(){
						vm.reload();
					});
				}else{
					alert(r.msg);
				}
			}
		});
	});
}  
function updatea(){
	var id = $("#userid").val();
	var cusname = $("#cusname").val();
	var password = $("#password").val();
	var passwordtwo = $("#passwordtwo").val();
	if(password != passwordtwo){
		alert("两次密码不一致!")
	}
	var candycount = $("#candycount").val();
	var bankid = $("#bankid").val();
	var openbranch = $("#openbranch").val();
	var peraccount = $("#peraccount").val();
	$.ajax({
		type: "POST",
	    url: baseURL + "sys/customer/updateall",
	    data: {"id":id,"cusname":cusname,"password":password,"candycount":candycount,"bankid":bankid,"openbranch":openbranch,"peraccount":peraccount},
	    success: function(r){
			if(r.code == 0){
				alert('操作成功', function(){
					vm.reload();
					$("#upcus").modal("hide");
				});
			}else{
				alert(r.msg);
			}
		}
	});
}
function Modify(id) {  

	$.ajax({
		type: "POST",
	    url: baseURL + "sys/customer/selechildren",
	    data: {"id":id},
	    success: function(r){
			if(r.code == 0){
				var setting = {
						data: {
							simpleData: {
								enable: true
							}
						}
					};
					var zNodes = r.list;
					$(document).ready(function(){
						$.fn.zTree.init($("#treeDemo"), setting, zNodes);
					});
			}else{
				alert(r.msg);
			}
		}
	});
	$("#newt").modal("show");
}  
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			content: null,
			starttime: null,
			subordinate: null,
			endtime: null,
			tid: null,
			cusname: null
		},
		showList: true,
		title: null,
		platformEntity: {},
		totalorder:[]
	},
	methods: {
		query: function () {
			
			vm.reload();
		},
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'username': vm.q.cusname},
                 page:page 
            }).trigger("reloadGrid");
		},
		
        validator: function () {
//           if(isBlank(vm.platformEntity.content)){
//                alert("标题不能为空");
//                return true;
//            }
//
//            if(isBlank(vm.platformEntity.title)){
//                alert("公告详情不能为空");
//                return true;
//            }

        },
		mounted1: function(){
			$.ajax({  //统计数量
				url:baseURL + "order/openorder/findCount",
				data:{'starttime': vm.q.starttime,'endtime': vm.q.endtime,'cusname': vm.q.cusname,},
				type:"post",//get  
				dataType:"json",  
				success:function(r){
					vm.totalorder=r.countOrderEntity
				},   
			});
		}
	},
//	mounted:function(){
//		$.ajax({  //统计数量
//			url:baseURL + "order/openorder/findCount",
//			type:"post",//get  
//			dataType:"json",  
//			success:function(r){
//				vm.totalorder=r.countOrderEntity
//				console.log(vm.totalorder)
//			},   
//		});
//	}
	
});